package com.study.demo01.config;

import cn.hutool.core.map.MapBuilder;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * <a href="https://img-blog.csdnimg.cn/d1612db2430f4c78a4bd4e93cf29b6e0.png">延迟交换机</a>
 */
@Configuration
public class DelayedExchangeConfig {

    public static final String delayed_queue_name = "delayed.queue";
    public static final String delayed_exchange_name = "delayed.exchange";
    public static final String delayed_routing_key = "delayed.routingkey";

    @Bean("delayedQueue")
    public Queue delayedQueue() {
        return QueueBuilder.durable(delayed_queue_name).build();
    }

    /**
     * 延迟交换机
     */
    @Bean(delayed_exchange_name)
    public CustomExchange delayedExchange() {
        Map<String, Object> build = MapBuilder.<String, Object>create()
                .put("x-delayed-type", "direct")
                .build();

        //名称,类型,是否需要持久化,是否自动删除
        CustomExchange customExchange = new CustomExchange(delayed_exchange_name, "x-delayed-message", true, false, build);
        return customExchange;
    }

    @Bean
    public Binding queueDelayBindy(@Qualifier("delayedQueue") Queue que, @Qualifier(delayed_exchange_name) CustomExchange exchange) {
        return BindingBuilder.bind(que).to(exchange).with(delayed_routing_key).noargs();
    }

}
